//
//  NewFeatureController.swift
//  sinaWeiBo
//
//  Created by weixq on 15/8/5.
//  Copyright © 2015年 weixq. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {

    private let imageCount = 4
    private let layout = HMFlowLayout()
    
    init(){
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeaturecell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }


    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeaturecell
        
        cell.imageIndex = indexPath.item
   
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 获得当前显示的item
         let Path = collectionView.indexPathsForVisibleItems().last!
        
        if Path.item == imageCount - 1{
            let cell = collectionView.cellForItemAtIndexPath(Path) as! NewFeaturecell
            cell.startButtonAnim()
        }
        
    }
}

class NewFeaturecell: UICollectionViewCell {
    
    private var imageIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    
    func clickStartButton() {
        // 发送通知，切换控制器
        NSNotificationCenter.defaultCenter().postNotificationName(HMRootViewControllerSwitchNotification, object: true)
    }
    
     func startButtonAnim(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
    
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        prepareUI()
    }
    
    private func prepareUI(){
        
        // 添加控件
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        
        // 添加约束
        iconView.ff_Fill(contentView)
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
        
    }
    
    private lazy var iconView = UIImageView()
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        // 根据背景图片自动调整大小
        button.sizeToFit()
        
        // 提示: 按钮的隐藏属性会因为复用工作不正常
//        button.hidden = true
        
        button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()

}

private class HMFlowLayout: UICollectionViewFlowLayout {

    private override func prepareLayout() {

        itemSize = collectionView!.bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
}

